var SceneRoadMark = function(options) {
	this.ctx = options.ctx;
	this.editor = options.editor;
	this.globe = options.editor.globe;
	this.sceneId = options.sceneId;
	this.viewer = this.globe.viewer;
	this.isOpen=false;
	this.editdIndex;
}

SceneRoadMark.prototype.draw = function() {
	var _this = this;
	_this.closeDraw();
	
	let options={
		id:this.globe.getUuid(),
		name:"good",
		code:"",
		type:'ERoadMark',
	}
	_this.drawRoadMark=this.globe.drawRoadMark(options);
	_this.drawRoadMark.addListener("finish",function(){
		_this.drawRoadMark=null;
	});
	_this.drawRoadMark.addListener("success",function(positions){
		if(positions){
            var formData={}; 
            formData['sceneRoadMark.id']=options.id;
            formData['sceneRoadMark.name']=options.name;
            formData['sceneRoadMark.code']=options.code;
            formData['sceneRoadMark.type']=options.type;
			formData['sceneRoadMark.coordinates']=JSON.stringify(positions);
            formData['sceneRoadMark.sceneId']=_this.sceneId;
            layer.load(2);
			var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/roadMark/save",formData, "json", null);
			layer.closeAll('loading');
			if(200==res.code){
				layer.msg(res?res.message:'系统异常!', {icon: 1});
				options.thing = {
					coordinates : JSON.stringify(positions),
				}
    			_this.globe.render(options);
			}else{
				 layer.msg(res?res.message:'系统异常!', {icon: 2});
			}
		}
	});
}

SceneRoadMark.prototype.edit=function(obj){
	var _this = this;
	_this.model=obj;
	if (_this.isOpen) {
		_this.closeEditTool();
		_this.createEditTool();
		_this.loadForm();
		return;
	}
	layer.load(2);
	var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/roadMark/edit", {}, "html", null);
	layer.closeAll('loading');
	if (!res) {
		layer.alert('系统异常！');
		return;
	}
	
	layer.open({
		id: 'editRoad',
		title: '属性编辑',
		type: 1,
		offset: '40px',
		area: '300px',
		content: res,
		shade: 0,
		offset: 'l',
		skin: 'layui-layer layui-layer-adminRight layui-layer-city',
		success: function(layero, dIndex) {
			_this.isOpen = true;
			_this.editdIndex = dIndex;
			$(layero).children('.layui-layer-content').css('overflow', 'hidden');
			$(layero).children('.layui-layer-content').css('height', 'calc(100% - 30px)');
			_this.loadForm();
			_this.createEditTool(obj);
			form.render();
		},
		end: function() {
			_this.isOpen = false;
			_this.editdIndex = null;
			_this.closeEditTool();
		}
	});
}

SceneRoadMark.prototype.loadForm = function(){
	var _this = this;
	var data=this.getFormData(_this.model);
	form.val('roadMarkEditForm', data);
	
	colorpicker.render({//间隔颜色
		elem: '#fontColor',
		color: (data&&data.fontColor)?data.fontColor:null,
		format: 'rgb',
		predefine: true,
		alpha: true,
		done: function(color) {
			$('#fontColor-input').val(color); //向隐藏域赋值
			color || this.change(color); //清空时执行 change
		},
		change: function(color) {
			 if(_this.model){
				 _this.model.updateFontColor(color);
			 }
		}
	});
	
	$('#sceneRoadMarkName').on('input', function() {
		 if(_this.model){
			 _this.model.updateName($("#sceneRoadMarkName").val());
		 }
	 });

	$('#sceneRoadMarkAngle').on('input', function() {
		 if(_this.model){
			 _this.model.updateAngle($("#sceneRoadMarkAngle").val());
		 }
	 });

	$('#sceneRoadMarkFont').on('input', function() {
		 if(_this.model){
			 _this.model.updateFont($("#sceneRoadMarkFont").val());
		 }
	 });

	$('#sceneRoadMarkFontSize').on('input', function() {
		 if(_this.model){
			 _this.model.updateFontSize($("#sceneRoadMarkFontSize").val());
		 }
	 });

	$('#sceneRoadMarkDeleteBtn').click(function() {
		layer.confirm('确定要删除模型吗？', {
			shade: .1
		}, function(index) {
			_this.delete(data);
		}, function(index) {
			layer.close(index);
			return false;
		});
	});
	
	form.on('submit(submitRoadMarkEditForm)', function(data) {
		layer.load(2);
		var formData = {};
		for (var key in data.field) {
			formData['sceneRoadMark.' + key] = data.field[key];
		}
		var res = common_ajax.ajaxFunc('post', _this.ctx + "/scene/roadMark/save", formData, "json", null);
		layer.closeAll('loading');
		if (res && 200 == res.code) {
			layer.msg(res.message, {icon: 1});
		} else {
			layer.msg(res.message, {icon: 2});
		}
		return false;
	});
}

SceneRoadMark.prototype.getFormData=function(obj){
	let data={};
	if(obj){
		data={
			id:obj.id,
			name:obj.name||'',
			code:obj.code||'',
			
			font:obj.thing.font||1,
			fontSize:obj.thing.fontSize||14,
			fontColor:obj.thing.fontColor||'',
			angle:obj.thing.angle||0,
		}	
	}
	return data;
}

SceneRoadMark.prototype.delete = function(obj){
	let _this=this;
	if(!obj){
		layer.alert('删除失败，刷新重试！');
		return;
	}
	layer.load(2);
	var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/layer/delete", {layerId:obj.id},"json", null);
	layer.closeAll('loading');
	if (res && 200==res.code) {
        layer.msg(res.message, {icon: 1});
		_this.close();
		_this.globe.remove(obj.id);
        return true;
    } else {
        layer.msg(res.message, {icon: 2});
        return false;
    }
}

SceneRoadMark.prototype.createEditTool=function(){
	let _this=this;
	_this.editRoadMarkTool=_this.globe.createEditRoadMark(_this.model);
	_this.editRoadMarkTool.on('change',function(e){
	});
}

SceneRoadMark.prototype.closeEditTool=function(){
	let _this=this;
	if(_this.editRoadMarkTool){
		_this.editRoadMarkTool.destroy();
		_this.editRoadMarkTool=null;
	}
}

SceneRoadMark.prototype.closeDraw =function(){
	var _this = this;
	if(_this.drawRoadMark){
		_this.drawRoadMark.destroy();
		_this.drawRoadMark=null;
	}
}

SceneRoadMark.prototype.close =function(){
	var _this = this;
	if(_this.editdIndex){
		layer.close(_this.editdIndex);
	}
}